package com.cljh.service.impl;

import com.cljh.bean.Distinct;
import com.cljh.bean.Product;
import com.cljh.bean.Products;
import com.cljh.controller.page.PageBean;
import com.cljh.controller.page.ProductList;
import com.cljh.controller.page.ProductPageBean;
import com.cljh.controller.page.UserPageBean;
import com.cljh.mapper.DistinctMapper;
import com.cljh.mapper.ProductMapper;
import com.cljh.mapper.ProductsMapper;
import com.cljh.mapper.UserMapper;
import com.cljh.service.ProductService;
import com.cljh.service.ProductsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ProductsServiceImpl implements ProductsService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ProductsMapper productsMapper;
    @Autowired
    DistinctMapper distinctMapper;
    @Override
    public boolean save(Products products) {
        Products products1 = productsMapper.selectByProducts(products);
        int save=0;
        if (products1==null){
             save= productsMapper.save(products);
        }
        return save>0;
    }

    @Override
    public boolean delete(Integer id) {
        return productsMapper.delete(id)>0;
    }

    @Override
    public boolean update(Products products) {


        return productsMapper.update(products)>0;
    }

    @Override
    public boolean deleteByIds(List<Integer> ids) {
        return productsMapper.deleteByIds(ids)>0;
    }

    @Override
    public UserPageBean selectById(Integer id) {
        Products p = productsMapper.selectById(id);

        UserPageBean userBean = null;
        if (p!=null){
            userBean=new UserPageBean();
            Integer distinctId = p.getProduct().getShowId();
            if (distinctId!=null){
                Distinct distinct = distinctMapper.selectById(distinctId);
                if (distinct!=null){
                    String productName = distinct.getProductName();
                    if (productName!=null){
                        userBean.setProductName(productName);
                    }
                    Double price = distinct.getPrice();
                    if (price!=null){
                        userBean.setPrice(price);
                    }
                    String types = distinct.getTypes();
                    if (types!=null){
                        userBean.setTypes(types);
                    }
                }
            }
            userBean.setId(p.getId());
            userBean.setUsername(p.getUser().getUsername());
            userBean.setPwd(p.getUser().getPwd());
            userBean.setName(p.getUser().getName());
            userBean.setGender(p.getUser().getGender());
            userBean.setVipNumber(p.getUser().getVipNumber());
            userBean.setMoney(p.getUser().getMoney());
            userBean.setImg(p.getUser().getImg());
            userBean.setBirthday(p.getUser().getBirthday());
//            userBean.setProductName(p.getProduct().getProductName());
            userBean.setProductSum(p.getProduct().getProductSum());
//            userBean.setPrice(p.getProduct().getPrice());
//            userBean.setTypes(p.getProduct().getTypes());
            userBean.setProductDesign(p.getProduct().getProductDesign());
            userBean.setProductColor(p.getProduct().getProductColor());
            userBean.setProductDetail(p.getProduct().getProductDetail());
            userBean.setProductMaterials(p.getProduct().getProductMaterials());
            userBean.setProductSize(p.getProduct().getProductSize());
            userBean.setSum(p.getSum());
            userBean.setCreateTime(p.getCreateTime());
            userBean.setUserId(p.getUserId());
            userBean.setProductId(p.getProductId());
        }
        return userBean;
    }

    @Override
    public List<ProductList> productsList() {
        List<Products> products = productsMapper.productsList();
        List<ProductList> pageBeans = new ArrayList<>();
        for (Products p : products) {
            ProductList userBean= new ProductList();

            Integer distinctId = p.getProduct().getShowId();

            if (distinctId!=null){
                Distinct distinct = distinctMapper.selectById(distinctId);
                if (distinct!=null){
                    String productName = distinct.getProductName();
                    if (productName!=null){
                        userBean.setProductName(productName);
                    }
                    Double price = distinct.getPrice();
                    if (price!=null){
                        userBean.setPrice(price);
                    }
                    String types = distinct.getTypes();
                    if (types!=null){
                        userBean.setTypes(types);
                    }
                }
            }
            userBean.setId(p.getId());
            userBean.setName(p.getUser().getName());
            userBean.setGender(p.getUser().getGender());
//            userBean.setProductName(p.getProduct().getProductName());
//            userBean.setPrice(p.getProduct().getPrice());
//            userBean.setTypes(p.getProduct().getTypes());
            userBean.setProductColor(p.getProduct().getProductColor());
            userBean.setProductMaterials(p.getProduct().getProductMaterials());
            userBean.setProductSize(p.getProduct().getProductSize());
            pageBeans.add(userBean);
        }
        return pageBeans;
    }

//    @Override
//    public boolean saveAll(List<Products> products) {
//        return false;
//    }

    @Override
    public PageBean<UserPageBean> productsConditionList(Integer currentPage, Integer pageSize, UserPageBean userPageBean) {
        //计算开始索引
        int begin = (currentPage - 1) * pageSize;

        String username = userPageBean.getUsername();
        if (username!=null&&username.length()>0){
            userPageBean.setUsername("%"+username+"%");
        }
        String productName = userPageBean.getProductName();
        if (productName!=null&&productName.length()>0){
            userPageBean.setProductName("%"+productName+"%");
        }

        List<Products> products = productsMapper.productsConditionList(begin, pageSize, userPageBean);

        List<UserPageBean> pageBeans = new ArrayList<>();
        for (Products p : products) {
            UserPageBean userBean= new UserPageBean();
            Integer distinctId = p.getProduct().getShowId();
            if (distinctId!=null){
                Distinct distinct = distinctMapper.selectById(distinctId);
                if (distinct!=null){
                    String productName1 = distinct.getProductName();
                    if (productName1!=null){
                        userBean.setProductName(productName1);
                    }
                    Double price = distinct.getPrice();
                    if (price!=null){
                        userBean.setPrice(price);
                    }
                    String types = distinct.getTypes();
                    if (types!=null){
                        userBean.setTypes(types);
                    }
                }
            }
            userBean.setId(p.getId());
            userBean.setUsername(p.getUser().getUsername());
            userBean.setPwd(p.getUser().getPwd());
            userBean.setName(p.getUser().getName());
            userBean.setGender(p.getUser().getGender());
            userBean.setVipNumber(p.getUser().getVipNumber());
            userBean.setMoney(p.getUser().getMoney());
            userBean.setImg(p.getUser().getImg());
            userBean.setBirthday(p.getUser().getBirthday());
//            userBean.setProductName(p.getProduct().getProductName());
            userBean.setProductSum(p.getProduct().getProductSum());
//            userBean.setPrice(p.getProduct().getPrice());
//            userBean.setTypes(p.getProduct().getTypes());
            userBean.setProductDesign(p.getProduct().getProductDesign());
            userBean.setProductColor(p.getProduct().getProductColor());
            userBean.setProductDetail(p.getProduct().getProductDetail());
            userBean.setProductMaterials(p.getProduct().getProductMaterials());
            userBean.setProductSize(p.getProduct().getProductSize());
            userBean.setSum(p.getSum());
            userBean.setCreateTime(p.getCreateTime());
            userBean.setUserId(p.getUserId());
            userBean.setProductId(p.getProductId());
            pageBeans.add(userBean);
        }

        int i = productsMapper.selectByCount(userPageBean);

        PageBean<UserPageBean> pageBean = new PageBean<>();
        pageBean.setRows(pageBeans);
        pageBean.setTotalCount(i);
        return pageBean;
    }

    @Override
    public List<Products> selectByUser(Integer id) {
        return productsMapper.selectByUser(id);
    }

    @Override
    public boolean deleteByUserAndProduct(Integer userId, Integer productId) {
        return productsMapper.deleteByUserAndProduct(userId,productId)>0;
    }
}
